<template>
  <view class="index-all">
    <Header title="工作台" :leftIconSize="0"></Header>
    <view class="tool-all">
      <view class="tool-text">
        <div class="title-left">常用工具</div>
        <div class="title-right"  @click="goEdit">
          <view class="title-btn">管理</view>
        </div>
      </view>
      <view class="tool-list">
        <view class="tool-item" v-for="(item, index) in applicationList" :key="index+ '_application'" @click="clickTo(item)">
          <view class="iconfont">
            <image class="index-menu-image" :src="imgMinioPath + item.query"></image>
          </view>
          <view class="tool-item-text">{{item.menuName ? item.menuName : '--'}}</view>
        </view>
      </view>
    </view>
    <view class="tool-all" v-for="(item, index) in menus" :key="index">
      <view class="tool-text">
        {{item.menuName ? item.menuName : '--'}}
      </view>
      <view class="tool-list">
        <view class="tool-item" @click="clickTo(child)" v-for="(child, index1) in item.children" :key="index1">
          <view class="circle" v-if="child.dataCount > 0">
            <text class="number">{{ child.dataCount > 99 ? '99+' : child.dataCount }}</text>
          </view>
          <image class="index-menu-image"  :src="imgMinioPath + child.query"></image>
          <view class="tool-item-text">{{child.menuName ? child.menuName : '--'}}</view>
        </view>
      </view>
    </view>
    <Tabbar></Tabbar>
  </view>
</template>

<script setup>
import {reactive, ref, getCurrentInstance, watchEffect} from 'vue';
import api from '@/api/user/user'
import supLawApi from "@/api/law/supLaw/supLaw";
import supTaskApi from "@/api/law/supTask/supTask";
import config from '@/common/config';
import {onLoad, onShow} from '@dcloudio/uni-app'
const {proxy} = getCurrentInstance();
let applicationList = ref([]);      // 获取应用数据
let menus = ref([]);                // 获取菜单数据
let imgMinioPath = ref(config.IMG_MINIO_PATH);
let menuButtons = ref([]);
// 获取用户菜单
let initMenu = async (e) => {
  let list = uni.getStorageSync('menus');
  menus.value = list.filter(item => {
    if (item.menuSource.indexOf(config.SCHOOL_MENU_SOURCE) !== -1) {
      return item;
    }
  })

  menuButtons.value = [];
  menus.value.forEach((item) => {
    menuButtons.value.push(...item.children)
  });
  // //查找气泡数量
  let checkMenuIds = config.CHECK_MENU_IDS;
  menuButtons.value.forEach(menu => {
    if (checkMenuIds.includes(menu.id)) {
      searchCount(menu);
    }
  });
}


// 获取我的工作台列表
let getApplicationList = async (e) => {
  let queryForm = {};
  let res = await proxy.http.asyncPost(api.getApplicationList, queryForm);

  if (res.code === 0) {
    let list = res.data;
    applicationList.value = list.filter(item => {
      if (item.menuSource.indexOf(config.SCHOOL_MENU_SOURCE) !== -1) {
        return item;
      }
    })
  }
}
const queryForm = reactive({
  params:{
    lawStatusList:[]
  },
  lawDetail: {
    schoolPropertiesCode: '', // 办学性质
    doSchoolTypeCode: '', // 办学类型
    countyId: '', // 地区
  },
});
const userInfo = ref(uni.getStorageSync('userInfo'));
// 首页气泡数
let searchCount = async (item) => {
  queryForm.pageNum = -1;
  queryForm.pageSize = -1;
  queryForm.createOrgId = userInfo.value.user.orgId;
  let dataCount = 0;
  if(item.id == config.CHECK_MENU_IDS[0]){//校园专项检查
    item.dataCount = await getSupLawPage('2','2');
  }else if(item.id == config.CHECK_MENU_IDS[1]){//校园日常检查
    item.dataCount = await getSupLawPage('2','1');
  }else if(item.id == config.CHECK_MENU_IDS[2]){//食品安全检查
    item.dataCount = await getSupLawPage('1','1');
  }else if(item.id == config.CHECK_MENU_IDS[3]){//配餐单位检查
    item.dataCount = await getSupLawPage('6','1');
  }else if(item.id == config.CHECK_MENU_IDS[4]){//校园自查
    item.dataCount = await getSupLawPage('4');
  }else if(item.id == config.CHECK_MENU_IDS[5]){//食堂自查
    item.dataCount = await getSupLawPage('3');
  }else if(item.id == config.CHECK_MENU_IDS[6]){//隐患自查
    item.dataCount = await getSupLawPage('5');
  }else if(item.id == config.CHECK_MENU_IDS[7]){//有害生物专项
    item.dataCount = await getSupLawPage('7','2');
  }else if(item.id == config.CHECK_MENU_IDS[8]){//有害生物日常
    item.dataCount = await getSupLawPage('7','1');
  }else if(item.id == config.CHECK_MENU_IDS[9]){//有害生物自查
    item.dataCount = await getSupLawPage('8');
  }
};
async function getSupLawPage(entryType,lawType) {
  queryForm.entryType = entryType;
  if(lawType){
    queryForm.lawStatus = null
    queryForm.params.lawStatusList = ["10","20","30"];
    queryForm.lawType = lawType;
  }else {
    queryForm.lawStatus = 20;
    queryForm.params.lawStatusList = []
    queryForm.lawType = null
  }
  let res = await proxy.http.asyncPost(supLawApi.getsupLawPage, queryForm);
  if (res.code === 0) {
    // if (lawType === '2') {
    //   let query = {
    //     finish_flag: '0'
    //   }
    //   let res2 = await proxy.http.asyncPost(supTaskApi.getSupTaskPage, query);
    //   if (res2.code === 0) {
    //     return res2.data.records.length + res.data.records.length;
    //   } else {
    //     await uni.showToast({title: res.msg, icon: 'none'});
    //     return 0;
    //   }
    // }
    return res.data.records.length || 0;
  } else {
    await uni.showToast({title: res.msg, icon: 'none'});
    return 0;
  }
}
getApplicationList();

let clickTo = async (item) => {
  console.info(item.path)
  if (item.path){
    uni.navigateTo({
      url: item.path
    })
  }else {
    uni.showToast({title: '敬请期待!', icon: 'none'});
  }
};

let goEdit = async () => {
  uni.navigateTo({
    url: '/packageBase/pages/base/user/applicationEdit',
    success: () => {
      //监听事件
      uni.$once("refresh", (data) => {
        getApplicationList();
      });
    },
  })
};
onShow(() => {
  uni.removeStorageSync('entryType');
  initMenu();
})
// #ifdef MP-WEIXIN
uni.hideTabBar();
// #endif
</script>

<style lang="scss" scoped>
.index-all {
  width: 100vw;
  height: 100vh;
  overflow: auto;
  background:#F8F9FC;
  .tool-all {
    @include form-box(24rpx);
      margin: 24rpx auto 12rpx;
    .tool-text {
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 70rpx;
      margin: 24rpx auto 48rpx;
      font-weight:bold;
      font-size: var(--uni-font-size-16);
    }
    .title-left {

      //width:50%;
      //margin-top: 10px;
      //margin-left: 10px;
    }
    .title-right {
      font-size: var(--uni-font-size-12);
      //margin-top: 10px;
      .title-btn {
        width: 96rpx;
        height: 48rpx;
        line-height: 48rpx;
        text-align: center;
        color: var(--uni-text-color-white) ;
        border-radius: 24rpx;
        background: #3775F6;
      }
    }
    .tool-list {
      width: 100%;
      display: flex;
      flex-wrap: wrap;
      align-items: flex-start;
      justify-content: flex-start;
      .tool-item {
        position: relative;
        display: flex;
        align-items: center;
        flex-direction: column;
        width: 25%;
        margin-bottom: 40rpx;
          .index-menu-image {
              width: 110rpx;
              height: 110rpx;
          }
        .tool-item-text {
          margin-top: 20rpx;
          font-size: var(--uni-font-size-12);
        }
      }
    }
  }
}
.circle {
  width: 40rpx;
  height: 40rpx;
  border-radius: 50%;
  background-color: red;
  top: 0;
  right: 10rpx;
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1;
}

.number {
  color: white;
  font-size: 22rpx;
}

</style>
